home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / AddOns / Components / TEECHART / Src Code / TeeOpenGL.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-10-24  |  9.0 KB  |  387 lines

  1. {******************************************}
  2. {      TeeChart Pro OpenGL Component       }
  3. {   Copyright (c) 1998 by David Berneda    }
  4. {         All Rights Reserved              }
  5. {******************************************}
  6. unit TeeOpenGL;
  7.  
  8. interface
  9.  
  10. uses
  11.   Windows, Messages, SysUtils, Classes, Graphics, TeeProcs, StdCtrls,
  12.   ExtCtrls, GLCanvas;
  13.  
  14. Const TeeOpenGLMsg_OpenGL='Open GL';
  15.  
  16. type
  17.   TTeeOpenGL=class;
  18.  
  19.   TGLPosition=class(TPersistent)
  20.   private
  21.     FX,FY,FZ : Double;
  22.     FOwner   : TTeeOpenGL;
  23.     Procedure SetX(Const Value:Double);
  24.     Procedure SetY(Const Value:Double);
  25.     Procedure SetZ(Const Value:Double);
  26.   public
  27.     Procedure Assign(Source:TPersistent); override;
  28.   published
  29.     property X:Double read FX write SetX;
  30.     property Y:Double read FY write SetY;
  31.     property Z:Double read FZ write SetZ;
  32.   end;
  33.  
  34.   TGLLight=class(TPersistent)
  35.   private
  36.     FColor     : TColor;
  37.     FPosition  : TGLPosition;
  38.     FVisible   : Boolean;
  39.     FOwner     : TTeeOpenGL;
  40.     Procedure SetColor(Value:TColor);
  41.     Procedure SetPosition(Value:TGLPosition);
  42.     procedure SetVisible(Value:Boolean);
  43.   public
  44.     Constructor Create(AOwner:TTeeOpenGL);
  45.     Destructor Destroy; override;
  46.     Procedure Assign(Source:TPersistent); override;
  47.     Function GLColor:GLMat;
  48.     Function GLPosition:GLMat;
  49.   published
  50.     property Color:TColor read FColor write SetColor default clSilver;
  51.     property Position:TGLPosition read FPosition write SetPosition;
  52.     property Visible:Boolean read FVisible write SetVisible default True;
  53.   end;
  54.  
  55.   TTeeOpenGL = class(TComponent)
  56.   private
  57.     { Private declarations }
  58.     FActive       : Boolean;
  59.     FLight        : TGLLight;
  60.     FShadeQuality : Boolean;
  61.     FShininess    : Double;
  62.     FTeePanel     : TCustomTeePanel;
  63.     FOnInit       : TNotifyEvent;
  64.     Procedure SetActive(Value:Boolean);
  65.     Procedure SetLight(Value:TGLLight);
  66.     Procedure SetShadeQuality(Value:Boolean);
  67.     Procedure SetTeePanel(Value:TCustomTeePanel);
  68.     Procedure SetShininess(Const Value:Double);
  69.     Procedure Activate;
  70.     Procedure OnCanvasInit(Sender:TObject);
  71.   protected
  72.     { Protected declarations }
  73.     procedure Notification( AComponent: TComponent;
  74.                             Operation: TOperation); override;
  75.     Procedure SetDoubleProperty(Var Variable:Double; Const Value:Double);
  76.     Procedure Repaint;
  77.   public
  78.     { Public declarations }
  79.     Constructor Create(AOwner:TComponent); override;
  80.     Destructor Destroy; override;
  81.   published
  82.     { Published declarations }
  83.     property Active:Boolean read FActive write SetActive default False;
  84.     property Light:TGLLight read FLight write SetLight;
  85.     property ShadeQuality:Boolean read FShadeQuality write SetShadeQuality default True;
  86.     property Shininess:Double read FShininess write SetShininess;
  87.     property TeePanel:TCustomTeePanel read FTeePanel write SetTeePanel;
  88.  
  89.     property OnInit:TNotifyEvent read FOnInit write FOnInit;
  90.   end;
  91.  
  92. Procedure Register;
  93.  
  94. implementation
  95.  
  96. {$R TEEGL.RES}
  97.  
  98. Uses Controls,TeeConst,TeCanvas,OpenGL2;
  99.  
  100. Procedure SetGLPanelCanvas(APanel:TCustomTeePanel; IsOpenGL:Boolean);
  101. begin
  102.   if IsOpenGL then
  103.   begin
  104.     APanel.Canvas:=TGLCanvas.Create;
  105.     APanel.BevelInner:=bvNone;
  106.     APanel.BevelOuter:=bvNone;
  107.   end
  108.   else
  109.   begin
  110.     APanel.Canvas:=TTeeCanvas3D.Create;
  111.     APanel.BevelInner:=bvNone;
  112.     APanel.BevelOuter:=bvRaised;
  113.   end;
  114. end;
  115.  
  116. (*
  117. type TGLClass=class
  118.      private
  119.        FPanel:TCustomTeePanel;
  120.        procedure CheckBox1Click(Sender: TObject);
  121.      end;
  122.  
  123. Var PrivateGLClass:TGLClass;
  124. procedure TGLClass.CheckBox1Click(Sender: TObject);
  125. begin
  126.   if Assigned(FPanel) then
  127.      SetGLPanelCanvas(FPanel,(Sender as TCheckBox).Checked);
  128. end;
  129.  
  130. Procedure TeeCommanderGLChain(Sender:TTeeCommander);
  131. Var FOpenGL:TCheckBox;
  132. begin
  133.   FOpenGL:=TCheckBox.Create(Sender);
  134.   With FOpenGL do
  135.   begin
  136.     Parent:=Sender;
  137.     Left:=Parent.Width-80;
  138.     Top:=8;
  139.     Caption:=TeeOpenGLMsg_OpenGL;
  140.     Width:=75;
  141.     if Sender.Panel<>nil then
  142.     begin
  143.       Checked:=Sender.Panel.Canvas is TGLCanvas;
  144.       PrivateGLClass:=TGLClass.Create;
  145.       PrivateGLClass.FPanel:=Sender.Panel;
  146.       OnClick:=PrivateGLClass.CheckBox1Click;
  147.     end;
  148.   end;
  149. end;
  150. *)
  151.  
  152. { TGLPosition }
  153. Procedure TGLPosition.SetX(Const Value:Double);
  154. begin
  155.   FOwner.SetDoubleProperty(FX,Value);
  156. end;
  157.  
  158. Procedure TGLPosition.SetY(Const Value:Double);
  159. begin
  160.   FOwner.SetDoubleProperty(FY,Value);
  161. end;
  162.  
  163. Procedure TGLPosition.SetZ(Const Value:Double);
  164. begin
  165.   FOwner.SetDoubleProperty(FZ,Value);
  166. end;
  167.  
  168. Procedure TGLPosition.Assign(Source:TPersistent);
  169. begin
  170.   if Source is TGLPosition then
  171.   With TGLPosition(Source) do
  172.   begin
  173.     Self.FX:=X;
  174.     Self.FY:=Y;
  175.     Self.FZ:=Z;
  176.   end
  177.   else inherited Assign(Source);
  178. end;
  179.  
  180. { TGLLight }
  181. Constructor TGLLight.Create(AOwner:TTeeOpenGL);
  182. begin
  183.   inherited Create;
  184.   FOwner:=AOwner;
  185.   FColor:=clGray;
  186.   FPosition:=TGLPosition.Create;
  187.   With FPosition do
  188.   begin
  189.     FOwner:=AOwner;
  190.     FX:=0;
  191.     FY:=100;
  192.     FZ:=0;
  193.   end;
  194.   FVisible:=True;
  195. end;
  196.  
  197. Destructor TGLLight.Destroy;
  198. begin
  199.   FPosition.Free;
  200.   inherited Destroy;
  201. end;
  202.  
  203. Procedure TGLLight.SetColor(Value:TColor);
  204. begin
  205.   if FColor<>Value then
  206.   begin
  207.     FColor:=Value;
  208.     FOwner.Repaint;
  209.   end;
  210. end;
  211.  
  212. Procedure TGLLight.SetPosition(Value:TGLPosition);
  213. begin
  214.   FPosition.Assign(Value);
  215. end;
  216.  
  217. procedure TGLLight.SetVisible(Value:Boolean);
  218. begin
  219.   if FVisible<>Value then
  220.   begin
  221.     FVisible:=Value;
  222.     FOwner.Repaint;
  223.   end;
  224. end;
  225.  
  226. Function TGLLight.GLColor:GLMat;
  227. var AColor:TColor;
  228. begin
  229.   AColor:=ColorToRGB(FColor);
  230.   result[0]:=GetRValue(AColor)/255.0;
  231.   result[1]:=GetGValue(AColor)/255.0;
  232.   result[2]:=GetBValue(AColor)/255.0;
  233.   result[3]:=1;
  234. end;
  235.  
  236. Function TGLLight.GLPosition:GLMat;
  237. begin
  238.   With FPosition do
  239.   begin
  240.     result[0]:=X;
  241.     result[1]:=Y;
  242.     result[2]:=Z;
  243.   end;
  244.   result[3]:=0;
  245. end;
  246.  
  247. Procedure TGLLight.Assign(Source:TPersistent);
  248. begin
  249.   if Source is TGLLight then
  250.   With TGLLight(Source) do
  251.   begin
  252.     Self.FColor:=Color;
  253.     Self.FPosition.Assign(Position);
  254.     Self.FVisible:=Visible;
  255.   end
  256.   else inherited Assign(Source);
  257. end;
  258.  
  259. { TTeeOpenGL }
  260. Constructor TTeeOpenGL.Create(AOwner:TComponent);
  261. begin
  262.   inherited Create(AOwner);
  263.   FLight:=TGLLight.Create(Self);
  264.   FShininess:=0.8;
  265.   FShadeQuality:=True;
  266. end;
  267.  
  268. Destructor TTeeOpenGL.Destroy;
  269. begin
  270.   FLight.Free;
  271.   if FActive and Assigned(FTeePanel) then SetGLPanelCanvas(FTeePanel,False);
  272.   inherited Destroy;
  273. end;
  274.  
  275. Procedure TTeeOpenGL.OnCanvasInit(Sender:TObject);
  276. var tmp:GLMat;
  277. begin
  278.   if FShadeQuality then glShadeModel(GL_SMOOTH)
  279.                    else glShadeModel(GL_FLAT);
  280.  
  281.   (Sender as TGLCanvas).UseMaterial:=FLight.Visible;
  282.  
  283.   if FLight.Visible then
  284.   begin
  285.     glEnable(GL_LIGHTING);
  286.     glEnable(GL_LIGHT0);
  287.     tmp:=FLight.GLColor;
  288. {    glLightModelfv(GL_LIGHT_MODEL_AMBIENT,  @tmp);}
  289.     glLightfv(GL_LIGHT0,  GL_AMBIENT, @tmp);
  290. {    glLightfv(GL_LIGHT0,  GL_DIFFUSE, @tmp);}
  291. {    glLightfv(GL_LIGHT0,  GL_SPECULAR, @tmp); }
  292.     tmp:=FLight.GLPosition;
  293.     glLightfv(GL_LIGHT0, GL_POSITION, @tmp);
  294.   end
  295.   else glDisable(GL_LIGHTING);
  296.   glMaterialf(GL_FRONT, GL_SHININESS, 128.0*FShininess);
  297.  
  298. //  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  299.   if Assigned(FOnInit) then FOnInit(Self);
  300. end;
  301.  
  302. Procedure TTeeOpenGL.SetShininess(Const Value:Double);
  303. begin
  304.   SetDoubleProperty(FShininess,Value);
  305. end;
  306.  
  307. Procedure TTeeOpenGL.SetShadeQuality(Value:Boolean);
  308. begin
  309.   if FShadeQuality<>Value then
  310.   begin
  311.     FShadeQuality:=Value;
  312.     Repaint;
  313.   end;
  314. end;
  315.  
  316. procedure TTeeOpenGL.Notification( AComponent: TComponent;
  317.                                    Operation: TOperation);
  318. begin
  319.   inherited Notification(AComponent, Operation);
  320.   if Operation=opRemove then
  321.      if Assigned(FTeePanel) and (AComponent=FTeePanel) then
  322.      begin
  323.        FTeePanel:=nil;
  324.        FActive:=False;
  325.      end;
  326. end;
  327.  
  328. Procedure TTeeOpenGL.Activate;
  329. begin
  330.   if Assigned(FTeePanel) then
  331.   begin
  332.     SetGLPanelCanvas(FTeePanel,FActive);
  333.     if FActive then (FTeePanel.Canvas as TGLCanvas).OnInit:=OnCanvasInit;
  334.   end;
  335. end;
  336.  
  337. Procedure TTeeOpenGL.SetLight(Value:TGLLight);
  338. begin
  339.   FLight.Assign(Value);
  340. end;
  341.  
  342. Procedure TTeeOpenGL.SetActive(Value:Boolean);
  343. begin
  344.   if FActive<>Value then
  345.   begin
  346.     FActive:=Value;
  347.     Activate;
  348.   end;
  349. end;
  350.  
  351. Procedure TTeeOpenGL.SetDoubleProperty(Var Variable:Double; Const Value:Double);
  352. begin
  353.   if Variable<>Value then
  354.   begin
  355.     Variable:=Value;
  356.     Repaint;
  357.   end;
  358. end;
  359.  
  360. Procedure TTeeOpenGL.Repaint;
  361. begin
  362.   if Assigned(FTeePanel) then FTeePanel.Repaint;
  363. end;
  364.  
  365. Procedure TTeeOpenGL.SetTeePanel(Value:TCustomTeePanel);
  366. begin
  367.   FTeePanel:=Value;
  368.   if Assigned(FTeePanel) then
  369.   begin
  370.     FTeePanel.FreeNotification(Self);
  371.     Activate;
  372.   end
  373.   else FActive:=False;
  374. end;
  375.  
  376. Procedure Register;
  377. begin
  378.   RegisterComponents(TeeMsg_TeeChartPalette, [TTeeOpenGL]);
  379. end;
  380.  
  381. {initialization
  382.   PrivateGLClass:=nil;
  383.   TeeCommanderChain:=TeeCommanderGLChain;
  384. finalization
  385.   PrivateGLClass.Free;}
  386. end.
  387.